package com.carlinfo.stu_ques.question.dao.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.carlinfo.stu_ques.common.dao.impl.BaseDaoImpl;
import com.carlinfo.stu_ques.common.util.FileUtil;
import com.carlinfo.stu_ques.demo.pojo.King;
import com.carlinfo.stu_ques.question.dao.IAnswerDao;
import com.carlinfo.stu_ques.question.pojo.Answer;

public class answerDaoImpl extends BaseDaoImpl<Answer> implements IAnswerDao
{
	/**
	 * 答案的保存路径
	 */
	private File file = new File(this.parentFile,"answer.dat");
	public answerDaoImpl()
	{
		readFile(file);
	}
	/**
	 * 保存一个问题
	 */
	@Override
	public int saveOneDao(Answer answer)
	{
		answer.setId(id);
		temId = answer.getId();
		int id = super.saveOneDao(answer);
		writeFile(file);
		return id;
	}
	/**
	 * 更新一条数据
	 */
	@Override
	public void updateOneDao(Answer answer)
	{
		temId = answer.getId();
		super.updateOneDao(answer);
		writeFile(file);
	}
	/**
	 * 删除一条数据
	 */
	@Override
	public void deleteOneDao(Answer answer)
	{
		temId = answer.getId();
		super.deleteOneDao(answer);
		writeFile(file);
	}
	
	@Override
	public boolean dbToFile(String path)
	{
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try
		{
			Map<Integer, Object> map = new TreeMap<Integer, Object>();
			List<Object> list = new ArrayList<Object>();
			File file = new File(path);
			FileOutputStream fos = new FileOutputStream(file+"\\answer.dat");
			FileUtil fileUtil = new FileUtil();
			/* 准备一条sql语句查询所有朝代 */
			String sql = "select * from answer;";
			conn = dbUtil.getConnection();
			pst = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			rs = pst.executeQuery();
			int id = 0;
			/* 将数据库中的数据取出来 */
			while(rs.next())
			{
				Answer answer = new Answer();		
				id = rs.getInt("id");
				answer.setId(id);
				answer.setStuId(rs.getInt("stuId"));
				answer.setQuestionId(rs.getInt("questionId"));;
				answer.setShiFou(rs.getInt("ans")==0 ? false : true);
				answer.setJieGuo(rs.getString("result"));
				answer.setEndTime(rs.getTimestamp("endTime"));
				answer.setCreateTime(rs.getTimestamp("createTime"));
				/* 将数据按照id放入map中 */
				map.put(id, answer);
			}
			/* 将sql语句关闭 */
			pst.close();
			rs.close();
			/* 查询id最大值 */
			sql = "select max(id) from answer;";
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			if(rs.next())
			{
				/* 将map和id最大值放入文件 */
				list.add(map);
				list.add(rs.getInt(1)+1);
				System.out.println("最大id："+rs.getInt(1));
			}
			/* 将数据写到文件中去 */
			fileUtil.writeObject(fos, list);
			return true;
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			/* 关闭链接 */
			dbUtil.closeAll(pst, conn, rs);
		}
		return false;
	}

	@Override
	public boolean fileToDb()
	{	
		List<Answer> list = this.selectListDao();
		if(list.size()!=0) {
			Connection conn = null;
			PreparedStatement pst = null;
			/* 每一次存之前,先将表清空 */
			String sql = "truncate table answer;";
			/* 获取连接 */
			conn = dbUtil.getConnection();
			try
			{
				pst = conn.prepareStatement(sql);
				pst.executeUpdate();
				pst.close();
				sql = "INSERT INTO `answer`(`stuId`, `questionId`, `ans`, `result`, `createTime`, `endTime`) VALUES (?, ?, ?, ?, ?, ?)";
				pst = conn.prepareStatement(sql);
				for (Answer answer : list)
				{
					pst.setObject(1,answer.getStuId());
					pst.setObject(2, answer.getQuestionId());
					pst.setObject(3, answer.isShiFou());
					pst.setObject(4, answer.getJieGuo());
					pst.setObject(5, answer.getCreateTime());
					pst.setObject(6, answer.getEndTime());
					pst.addBatch();				
				}
				pst.executeBatch();
				pst.clearBatch();
				return true;
			} catch (SQLException e)
			{
				e.printStackTrace();
			} finally
			{
				/* 关闭链接 */
				dbUtil.closeAll(pst, conn, null);
			}
		} else
		{
			System.out.println("该文件是空的，没有数据可以插入");
		}
		return false;
	}
}
